package com.lyuewei.ringtone.ui; // 替换为你的 UI 包名

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Switch;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;

import com.elvishew.xlog.XLog;
import com.lyuewei.ringtone.R; // 导入你的资源文件
import com.lyuewei.ringtone.data.RingerSetting;

import java.util.Locale;

public class RingerSettingAdapter extends ListAdapter<RingerSetting, RingerSettingAdapter.SettingViewHolder> {

    private OnItemLongClickListener longClicklistener;
    private OnSettingEnabledChangeListener enabledChangeListener; // 新增监听器

    public RingerSettingAdapter() {
        super(DIFF_CALLBACK);
    }

    private static final DiffUtil.ItemCallback<RingerSetting> DIFF_CALLBACK = new DiffUtil.ItemCallback<RingerSetting>() {
        @Override
        public boolean areItemsTheSame(@NonNull RingerSetting oldItem, @NonNull RingerSetting newItem) {
            return oldItem.getId() == newItem.getId();
        }

        @Override
        public boolean areContentsTheSame(@NonNull RingerSetting oldItem, @NonNull RingerSetting newItem) {
            return oldItem.equals(newItem); // 需要在 RingerSetting 类中实现 equals() 和 hashCode()
        }
    };

    @NonNull
    @Override
    public SettingViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_ringer_setting, parent, false);
        return new SettingViewHolder(itemView);
    }

    @SuppressLint({"SetTextI18n", "DefaultLocale"})
    @Override
    public void onBindViewHolder(@NonNull SettingViewHolder holder, int position) {
        RingerSetting currentSetting = getItem(position);
        XLog.d("Adapter Binding position: " + position + ", Enabled: " + currentSetting.isEnabled());
        holder.textViewName.setText(currentSetting.getName());
        holder.textViewPhoneNumber.setText( currentSetting.getPhone());
        String details = String.format(Locale.getDefault(),
                "音量: %d%%, 间隔: %d分, 次数: %d次, 时间: %02d:%02d - %02d:%02d",
                currentSetting.getVolume(),
                currentSetting.getInterval(),
                currentSetting.getTimes(),
                currentSetting.getStartHour(),
                currentSetting.getStartMinute(),
                currentSetting.getEndHour(),
                currentSetting.getEndMinute());
        holder.textViewDetails.setText(details);
        // 设置开关状态
        holder.switchEnabled.setOnCheckedChangeListener(null);
        holder.switchEnabled.setChecked(currentSetting.isEnabled());
        // 为开关设置监听器
        // 注意：为防止重复触发，先移除旧监听器（如果存在）

        holder.switchEnabled.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (enabledChangeListener != null) {
                XLog.d("Adapter Switch checked changed for position: " + position + ", New state: " + isChecked + ", Setting ID: " + currentSetting.getId());

                // 调用监听器，将当前设置对象和新的启用状态传递出去
                enabledChangeListener.onSettingEnabledChange(currentSetting, isChecked);
            }
        });
      }

    public RingerSetting getSettingAt(int position) {
        return getItem(position);
    }

    public   class SettingViewHolder extends RecyclerView.ViewHolder {
        private final TextView textViewName;
        private final TextView textViewPhoneNumber;
        private final TextView textViewDetails;
        private final Switch switchEnabled;
        public SettingViewHolder(@NonNull View itemView) {
            super(itemView);
            textViewName = itemView.findViewById(R.id.textViewName);
            textViewPhoneNumber = itemView.findViewById(R.id.textViewPhoneNumber);
            textViewDetails = itemView.findViewById(R.id.textViewDetails);
            switchEnabled = itemView.findViewById(R.id.switchEnabled);
            itemView.setOnLongClickListener(v -> {
                int position = getAdapterPosition();
                if (longClicklistener != null && position != RecyclerView.NO_POSITION) {
                    longClicklistener.onItemLongClick(getItem(position));
                }
                return true;
            });
        }
    }


    public interface OnItemLongClickListener {
        void onItemLongClick(RingerSetting setting);
    }


    public void setOnItemLongClickListener(OnItemLongClickListener longClicklistener) {
        this.longClicklistener = longClicklistener;
    }
    public interface OnSettingEnabledChangeListener {
        void onSettingEnabledChange(RingerSetting setting, boolean isEnabled);
    }

    public void setOnSettingEnabledChangeListener(OnSettingEnabledChangeListener listener) {
        this.enabledChangeListener = listener;
    }
}
